﻿using System;
using NaverOpenApiLibrary.Models;
using System.Collections.Generic;

namespace NaverOpenApiLibrary.Services
{
    /// <summary>
    /// News search service
    /// For more info: http://dev.naver.com/openapi/apis/search/news
    /// </summary>
    public class NewsSearchService : NaverApiService<NewsSearchWrapper>
    {
        // url pattrn: http://openapi.naver.com/search?key={0}&query={1}&target=news&start={2}1&display={3}
        protected const string URL_NEWS_SEARCH_SERVICE = "http://openapi.naver.com/search";

        private int _start;
        private int _display;
        private string _keyword;

        public NewsSearchService(string apiKey)
            : base(apiKey, URL_NEWS_SEARCH_SERVICE)
        { 
            // do nothing...
        }

        /// <summary>
        /// Do search for news / 뉴스검색시 호출하는 API
        /// Gets first top 10 results / 검색상위 10개 기사를 조회 함
        /// </summary>
        /// <param name="keyword">Keyword to search / 검색할 키워드</param>
        public void GetNews(string keyword)
        {
            GetNews(keyword, 1, 10);
        }

        /// <summary>
        /// Do search for news / 뉴스검색시 호출하는 API
        /// </summary>
        /// <param name="keyword">Keyword to search / 검색할 키워드</param>
        /// <param name="start">start index to search</param>
        /// <param name="display">number of articles to get at once</param>
        public void GetNews(string keyword, int start, int display)
        {
            this._keyword = Uri.EscapeUriString(keyword);
            this._start = start;
            this._display = display;
            DoApiService();
        }

        protected override IDictionary<string, string> GetApiUriParameters()
        {
            return new Dictionary<string, string>()
            {
                {"query", this._keyword},
                {"target", "news"},
                {"start", this._start.ToString()},
                {"display", this._display.ToString()}
            };
        }

        protected override NewsSearchWrapper Deserialize(string rawData)
        {
            return base.ParseXmlResult(rawData);
        }
    }
}
